<?php
/**
 * Created by PhpStorm.
 * User: ZhangWei
 * Date: 2018/4/23
 * Time: 14:27
 */

namespace App\Http\Controllers\Api\V1;


use App\Http\Resources\DocArticleCollection;
use App\Http\Resources\DocCollection;
use App\Models\Doc;
use App\Models\DocArticle;
use Illuminate\Http\Request;

class CenterController extends BaseController
{
    public function my_doc(Request $request)
    {
        $mode = $request->input("mode", 0);
        $status = $request->input("status", 0);
        $user = $this->getUser();
        $doc = Doc::query()->where('user_id', $user->id);
        $doc->with([
            'category',
            'pages',
            'articles',
            'tags',
            'listened'
        ])->orderBy('created_at', 'desc');
        switch ($mode) {
            case 0:
                $doc->where('mode', 'docs');
                break;
            case 1:
                $doc->where('mode', 'articles');
                break;
        }
        switch ($status) {
            case 0:
                $doc->withAnyStatus();
                break;
            case 2:
                $doc->pending();
                break;
            case 3:
                $doc->rejected();
                break;
        }
        $page = $doc->paginate(8);
        $data['docs'] = DocCollection::make($page)->hide(['user', 'buy_info', 'last_update', 'share_data', 'last_update', 'subscribe_count']);
        return $this->success($data);

    }

    public function doc_articles(Request $request)
    {
        $id = $this->getId();
        $user = $this->getUser();
        $doc = Doc::query()->withAnyStatus()->where('user_id', $user->id)->findOrFail($id);
        $article = DocArticle::query()->where('user_id', $user->id)->where('doc_id', $doc->id)->orderByDesc('order');

        $page = $article->paginate();
        $data = DocArticleCollection::make($page);
        return $this->success($data);

    }

}